home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Online / Qpopper / flock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-20  |  2.1 KB  |  82 lines

  1. /*
  2.  * Copyright (c) 1989 Mark Davies
  3.  * Copyright (c) 1990 Andy Linton
  4.  * Copyright (c) 1990 Victoria University of Wellington.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted provided
  8.  * that: (1) source distributions retain this entire copyright notice and
  9.  * comment, and (2) distributions including binaries display the following
  10.  * acknowledgement:  ``This product includes software developed by the
  11.  * Victoria University of Wellington, New Zealand and its contributors''
  12.  * in the documentation or other materials provided with the distribution
  13.  * and in all advertising materials mentioning features or use of this
  14.  * software.
  15.  * Neither the name of the University nor the names of its contributors may
  16.  * be used to endorse or promote products derived from this software without
  17.  * specific prior written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  */
  22.  
  23. #if defined(SYSV) || defined(ISC)
  24.  
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <errno.h>
  28. #ifdef ISC
  29. # include <net/errno.h>
  30. #endif
  31. #include "flock.h"               /* defines normally in <sys/file.h> */
  32.  
  33.  
  34. #if defined(F_SETLK) && defined(F_SETLKW)
  35.  
  36. flock(fd, operation)
  37. int fd, operation;
  38. {
  39.     int op, ret;
  40.     struct flock arg;
  41.     extern int errno;
  42.  
  43.     op = (LOCK_NB & operation) ? F_SETLK : F_SETLKW;
  44.     
  45.     arg.l_type = (LOCK_EX & operation) ? F_WRLCK :
  46.         (LOCK_SH & operation) ? F_RDLCK : F_UNLCK;
  47.     arg.l_whence = 0;
  48.     arg.l_start = 0;
  49.     arg.l_len = 0;
  50.     arg.l_pid = 0;
  51.     
  52.     if ((ret = fcntl(fd, op, &arg)) == -1) {
  53.         if (errno == EACCES || errno == EAGAIN)
  54.             errno = EWOULDBLOCK;
  55.     }
  56.     return (ret);
  57. }
  58.  
  59. #else
  60.  
  61. flock(fd, operation)
  62. int fd, operation;
  63. {
  64.     int op, ret;
  65.     extern int errno;
  66.  
  67.     op = (LOCK_UN & operation) ? F_ULOCK :
  68.       (LOCK_NB & operation) ? F_TLOCK : F_LOCK;
  69.  
  70.     if ((ret = lockf(fd, op, 0)) == -1) {
  71.         if (errno == EACCES || errno == EAGAIN)
  72.             errno = EWOULDBLOCK;
  73.     }
  74.  
  75.     return (ret);
  76. }
  77.  
  78. #endif /* F_SETLK && F_SETLKW */
  79.  
  80. #endif /* SYSV || ISC */
  81.  
  82.